<?php

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/sql_exception.php");

require_once("util/connection.php");

require_once("model/autor/dao/autor_dao.php");
require_once("model/autor/to/autor_to.php");


class Mysql_autor_dao extends Autor_dao{

	public function create($autor_to) {
		if ($this->exist($autor_to->get_login())) {
			throw new Duplicate_instance_exception(get_class($this));
		}
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($autor_to->get_login());
			$password = mysql_real_escape_string($autor_to->get_password());
			$email = mysql_real_escape_string($autor_to->get_email());
			
			$result = mysql_query("insert into autor (login,password,email)
									values ('$login','$password','$email')",
									$link);			
			
			if (!$result || (mysql_affected_rows($link) != 1)) 
				throw new Sql_exception("Not row inserted in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
			
			return $autor_to;
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	
	public function exist($login) {

		try {
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor 
									where login like '$login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	
	public function find_by_login_autor($login) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select * from autor 
									where login like '$login'", $link);
									
			if (mysql_num_rows($result) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_num_rows($result) != 1) 
				throw new Sql_exception(get_class($this));

			$aux = mysql_fetch_assoc($result);
			Connection::singleton()->transaction_commit($link);
			return new Autor_to($aux['login'], $aux['password'], $aux['email']);
			
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	
	public function update($autor_to) {
	
		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($autor_to->get_login());
			$email = mysql_real_escape_string($autor_to->get_email());
			
			$result = mysql_query("update autor 
									set email='$email' 
									where login like '$login'", $link);
							
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);			
			return $autor_to;
			
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}		
	}
	
	public function update_password($login, $password) {
		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			$password = mysql_real_escape_string($password);
			
			$result = mysql_query("update autor 
									set password='$password' 
									where login like '$login'", $link);
							
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);			
			
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
	
	public function remove($login) {	
		try {			
			$link = Connection::singleton()->transaction_begin();
			
			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("delete from autor
									where login like '$login'", $link);
									
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	
}
?>